# 给定一个m x n大小的矩阵（m行，n列），按螺旋的顺序返回矩阵中的所有元素。

from typing import List


def func(matrix: List[List[int]]) -> List[int]:
    if len(matrix) == 0:
        return
    result = []
    top, bottom = 0, len(matrix) - 1
    left, right = 0, len(matrix[0]) - 1
    # rows, cols = len(matrix), len(matrix[0])
    while left <= right and top <= bottom:
        for i in range(left, right+1):
            result.append(matrix[top][i])
        for j in range(top+1, bottom+1):
            result.append(matrix[j][right])
        if left < right and top < bottom:
            for i in range(right-1, left, -1):
                result.append(matrix[bottom][i])
            for j in range(bottom, top, -1):
                result.append(matrix[j][left])
        left += 1
        top += 1
        right -= 1
        bottom -= 1
    return result


if __name__ == '__main__':
    t = [[1,2],[3,4]]
    print(func(t))